require("tmysql4")

if ( KKB.DB == nil ) then
	KKB.DB, KKB.DBError = tmysql.initialize( KKB.DATABASE_SERVER, KKB.DATABASE_USERNAME, KKB.DATABASE_PASSWORD, KKB.DATABASE_DATABASE, KKB.DATABASE_PORT )
end

if ( KKB.DB ) then
	print( "[KKB][MySQL]: Connected!")
elseif ( KKB.DBError ) then
	print( "[KKB][MySQL][ERROR]: "..KKB.DBError )
end

function KKB.DBQuery( Sql, Callback )
	KKB.DB:Query( Sql, function( result, status, err )
		if ( status ) then
			if ( Callback != nil ) then
				Callback( result )
			end
		else
			print( "[KKB][MySQL][ERROR]: "..err )
			print( Sql )
		end
	end, 1 )
end

local function KKB_Setup_UserGroups()
	local SQL = "CREATE TABLE IF NOT EXISTS KKB_UserGroups ( ID INT UNSIGNED NOT NULL AUTO_INCREMENT, UserGroup VARCHAR(45) NOT NULL, SpawnSettings TEXT NOT NULL, PhysgunSettings TEXT NOT NULL, ToolgunSettings TEXT NOT NULL, GravcannonSettings TEXT NOT NULL, DamageSettings TEXT NOT NULL, UseSettings TEXT NOT NULL, OtherSettings TEXT NOT NULL, DonateSettings TEXT NOT NULL, PRIMARY KEY(ID) )"
	local function Callback( result )
		print( "[KKB][MySQL]: User Groups Table OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

local function KKB_Setup_CustomUsers()
	local SQL = "CREATE TABLE IF NOT EXISTS KKB_CustomUsers ( ID INT UNSIGNED NOT NULL AUTO_INCREMENT, UniqueID VARCHAR(45) NOT NULL UNIQUE, UserGroup VARCHAR(45) NOT NULL, Nick TEXT NOT NULL, PRIMARY KEY(ID) )"
	local function Callback( result )
		print( "[KKB][MySQL]: Custom Users Table OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

local function KKB_Setup_Damage()
	local SQL = "CREATE TABLE IF NOT EXISTS KKB_Damage ( ID INT UNSIGNED NOT NULL AUTO_INCREMENT, DamageTypes TEXT NOT NULL, PRIMARY KEY(ID) )"
	local function Callback( result )
		print( "[KKB][MySQL]: Damage Table OK!" )
		local SQL2 = "SELECT * FROM KKB_Damage"
		local function Callback2( result2 )
			if ( result2[1] == nil ) then
				local JSON = tmysql.escape(util.TableToJSON(KKB.Damage))
				local SQL3 = "INSERT INTO KKB_Damage (DamageTypes) VALUES ('"..JSON.."')"
				local function Callback3( result3 )
					print( "[KKB][MySQL]: Damage Table: Initial Values SET!" )
				end
				KKB.DBQuery( SQL3, Callback3 )
			end
		end
			KKB.DBQuery( SQL2, Callback2 )
	end
	KKB.DBQuery( SQL, Callback )
end

local function KKB_Setup_Settings()
	local SQL = "CREATE TABLE IF NOT EXISTS KKB_Settings ( ID INT UNSIGNED NOT NULL AUTO_INCREMENT, SettingTypes TEXT NOT NULL, PRIMARY KEY(ID) )"
	local function Callback( result )
		print( "[KKB][MySQL]: Settings Table OK!" )
		local SQL2 = "SELECT * FROM KKB_Settings"
		local function Callback2( result2 )
			if ( result2[1] == nil ) then
				local JSON = tmysql.escape(util.TableToJSON(KKB.Settings))
				local SQL3 = "INSERT INTO KKB_Settings ( SettingTypes ) VALUES ( '"..JSON.."' )"
				local function Callback3( result3 )
					print( "[KKB][MySQL]: Settings Table: Initial Values SET!" )
				end
				KKB.DBQuery( SQL3, Callback3 )
			end
		end
			KKB.DBQuery( SQL2, Callback2 )
	end
	KKB.DBQuery( SQL, Callback )
end

local function KKB_Setup_Models()
	local SQL = "CREATE TABLE IF NOT EXISTS KKB_Models ( ID INT UNSIGNED NOT NULL AUTO_INCREMENT, ModelName TEXT NOT NULL, PRIMARY KEY(ID) )"
	local function Callback( result )
		print( "[KKB][MySQL]: Blocked Models Table OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

local function KKB_Setup_Entitys()
	local SQL = "CREATE TABLE IF NOT EXISTS KKB_Entitys ( ID INT UNSIGNED NOT NULL AUTO_INCREMENT, EntityName VARCHAR(45) NOT NULL UNIQUE, UserGroups TEXT NOT NULL, Options TEXT NOT NULL, PRIMARY KEY(ID) )"
	local function Callback( result )
		print( "[KKB][MySQL]: Entitys Table OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

local function KKB_Setup_NPCs()
	local SQL = "CREATE TABLE IF NOT EXISTS KKB_NPCs ( ID INT UNSIGNED NOT NULL AUTO_INCREMENT, NPCName VARCHAR(45) NOT NULL UNIQUE, UserGroups TEXT NOT NULL, Options TEXT NOT NULL, PRIMARY KEY(ID) )"
	local function Callback( result )
		print( "[KKB][MySQL]: NPCs Table OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

local function KKB_Setup_Vehicles()
	local SQL = "CREATE TABLE IF NOT EXISTS KKB_Vehicles ( ID INT UNSIGNED NOT NULL AUTO_INCREMENT, VehicleName VARCHAR(45) NOT NULL UNIQUE, UserGroups TEXT NOT NULL, Options TEXT NOT NULL, PRIMARY KEY(ID) )"
	local function Callback( result )
		print( "[KKB][MySQL]: Vehicles Table OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

local function KKB_Setup_Weapons()
	local SQL = "CREATE TABLE IF NOT EXISTS KKB_Weapons ( ID INT UNSIGNED NOT NULL AUTO_INCREMENT, WeaponName VARCHAR(45) NOT NULL UNIQUE, UserGroups TEXT NOT NULL, Options TEXT NOT NULL, PRIMARY KEY(ID) )"
	local function Callback( result )
		print( "[KKB][MySQL]: Weapons Table OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

local function KKB_Setup_Tools()
	local SQL = "CREATE TABLE IF NOT EXISTS KKB_Tools ( ID INT UNSIGNED NOT NULL AUTO_INCREMENT, ToolName VARCHAR(45) NOT NULL UNIQUE, UserGroups TEXT NOT NULL, Options TEXT NOT NULL, PRIMARY KEY(ID) )"
	local function Callback( result )
		print( "[KKB][MySQL]: Tools Table OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

function KKB.DBSetup()
	KKB_Setup_UserGroups()
	KKB_Setup_CustomUsers()
	KKB_Setup_Damage()
	KKB_Setup_Settings()
	KKB_Setup_Models()
	KKB_Setup_Entitys()
	KKB_Setup_NPCs()
	KKB_Setup_Vehicles()
	KKB_Setup_Weapons()
	KKB_Setup_Tools()
end